package me.pcqpcq.android.tvlauncher.fragment;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;
import me.pcqpcq.android.tvlauncher.BaseFragment;
import me.pcqpcq.android.tvlauncher.R;
import me.pcqpcq.android.tvlauncher.helper.ProjectHelper;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import java.io.File;

/**
 *
 * 视频播放区域
 * @author pcqpcq
 * @version 1.0.0
 * @since 13-10-21 下午11:42
 */
public class VideoFragment extends BaseFragment implements MediaPlayer.OnCompletionListener,
        MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {

    private VideoView mVideoView;
    private ProgressBar mProgressBar;

    private String mVideoUrl;

    private int index;
    private String[] urls = new String[]{
            "http://flv.bn.netease.com/videolib3/1310/31/SOucP0376/SD/SOucP0376-mobile.mp4",
            "http://www.mylifi.cn/ad.mp4",
            "http://forum.ea3w.com/coll_ea3w/attach/2008_10/12237832415.3gp"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_video, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.setBackgroundColor(0xFF8DDB2A);

        mVideoView = (VideoView) view.findViewById(R.id.viv_video_area);
        mProgressBar = (ProgressBar) view.findViewById(R.id.prb_video_loading);

        // init 1
        initVideoView();
        // init 2
        initVideoUrl(urls[index++ % urls.length]);
    }

    @Override
    public void onDestroyView() {
        playFinish();
        super.onDestroyView();
    }

    @Override
    public void onCompletion(MediaPlayer arg0) {
        showLoading();
        initVideoUrl(urls[index++ % urls.length]);
        // play again
        playVideo();
    }

    @Override
    public void onPrepared(MediaPlayer mediaplayer) {
        hideLoading();
        playVideo();
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        hideLoading();
        Toast.makeText(getActivity(), "播放失败！", Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mVideoView != null) {
            mVideoView.resume();
        }
    }

    @Override
    public void onPause() {
        if (mVideoView != null) {
            mVideoView.pause();
        }
        super.onPause();
    }

    /**
     * 初始化播放器
     */
    private void initVideoView() {
        if (mVideoView != null) {
            mVideoView.setMediaController(new MediaController(getActivity()));
            mVideoView.setOnCompletionListener(this);
            mVideoView.setOnPreparedListener(this);
            mVideoView.setOnErrorListener(this);
        }
    }

    /**
     * 初始化视频地址
     */
    private void initVideoUrl(String url) {
        if (url != null) {
            mVideoView.setVideoURI(Uri.parse(url));
        }
    }

    /**
     * 开始播放
     */
    private void playVideo() {
        if (mVideoView != null) {
            mVideoView.requestLayout();
            mVideoView.start();
        }
    }

    /**
     * 播放完成
     */
    private void playFinish() {
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
    }

    /**
     * 显示加载中
     */
    private void showLoading() {
        if (mProgressBar != null) {
            mProgressBar.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏加载中
     */
    private void hideLoading() {
        if (mProgressBar != null) {
            mProgressBar.setVisibility(View.GONE);
        }
    }

}
